//
//  AudiHistoryViewController.m
//  AudiHistory
//
//  Created by niko on 11-8-14.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AudiHistoryViewController.h"
#import "ImageScrollView.h"
#import "CMenuBar.h"
#import "CPageDataStruct.h"
#import "IndexScrollView.h"
static AudiHistoryViewController *sharedAudiHistoryViewControllerSingletonManager = nil;
@implementation AudiHistoryViewController
+(AudiHistoryViewController*)SharedObj
{
	@synchronized(self) 
	{
		if (sharedAudiHistoryViewControllerSingletonManager == nil) 
		{
			[[self alloc] init]; // assignment not done here
		}
	}
	return sharedAudiHistoryViewControllerSingletonManager;
}

- (void)SetCurNewPage:(int)nCurNewPage
{
	m_nCurNewPage = nCurNewPage;
}

- (int)GetCurPage
{
	return m_nCurPage;
}

- (int)GetCurNewPage
{
	return m_nCurNewPage;
}

#pragma mark -
#pragma mark MenuBar
- (void)initMenuBar
{	
	CGRect rect = CGRectMake(0, 
							 [self frameForPagingScrollView].size.height * 0.94, 
							 [self frameForPagingScrollView].size.width, 
							 [self frameForPagingScrollView].size.height * 0.06);
	m_pMenuBar = [[CMenuBar alloc] initWithFrame:rect];
	//[self.view insertSubview:pMenuBar atIndex:1];
	[self.view addSubview:m_pMenuBar];
	[m_pMenuBar Build];
	
	//UIWindow* window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
	//[window addSubview:m_pMenuBar];
	//[window insertSubview:m_pMenuBar aboveSubview:m_pPagingScrollView];
}

#pragma mark -
#pragma mark Index
- (void)initIndexBar
{	
	CGRect rect = CGRectMake(0, 
							 ([self frameForPagingScrollView].size.height * 0.94) - ([self frameForPagingScrollView].size.height * 0.12), 
							 [self frameForPagingScrollView].size.width, 
							 [self frameForPagingScrollView].size.height * 0.12);
	m_pIndexView = [[IndexView alloc] initWithFrame:rect];
	//[self.view insertSubview:pMenuBar atIndex:1];
	//[self.view addSubview:m_pIndexView];
	[m_pIndexView Build];
}

static BOOL bIsShowIndexBar = TRUE;

- (void)ShowIndexBar
{	
	if (bIsShowIndexBar)
	{
		[self.view addSubview:m_pIndexView];
		bIsShowIndexBar = FALSE;
	}
	else
	{
		[m_pIndexView removeFromSuperview];
		bIsShowIndexBar = TRUE;
	}
}

- (void)HideIndexBar
{
	[m_pIndexView removeFromSuperview];
	bIsShowIndexBar = TRUE;
}

#pragma mark -
#pragma mark Tiling and page configuration

- (void)tilePages 
{
    // Calculate which pages are visible
    CGRect visibleBounds = m_pPagingScrollView.bounds;
    int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
    int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
    firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
    lastNeededPageIndex  = MIN(lastNeededPageIndex, [self imageCount] - 1);
	
	m_nCurNewPage = lastNeededPageIndex;
	if (firstNeededPageIndex == lastNeededPageIndex)
	{
		[self HideIndexBar];
//		if(nil != m_pPage)
//		{
//			if (lastNeededPageIndex == m_pPage.nIndex)
//			{
//				return;
//			}
//			[m_pPage removeFromSuperview];
//			RELEASE_OBJECT(m_pPage);
//			
//		}
//	
//		m_pPage = [[ImageScrollView alloc] initWithFrame:[self frameForPageAtIndex:firstNeededPageIndex]];
//		m_pPage.nIndex = lastNeededPageIndex;
//		//m_pPage.frame = [self frameForPageAtIndex:m_pPage.nIndex];
//		[m_pPage LoadPage:m_pPage.nIndex];
//		[m_pPagingScrollView addSubview:m_pPage];
	}
}
#pragma mark -
#pragma mark  Frame calculations
#define PADDING  10
- (NSUInteger)imageCount
{
    return [[CPageDataStructTable SharedObj] GetPageCount];
}

- (CGRect)frameForPagingScrollView 
{
    CGRect frame = [[UIScreen mainScreen] bounds];
	frame = CGRectMake(0, 0, frame.size.height, frame.size.width);
    //frame.origin.x -= PADDING;
    //frame.size.width += (2 * PADDING);
    return frame;
}

- (CGRect)frameForPageAtIndex:(NSUInteger)index 
{
    // We have to use our paging scroll view's bounds, not frame, to calculate the page placement. When the device is in
    // landscape orientation, the frame will still be in portrait because the pagingScrollView is the root view controller's
    // view, so its frame is in window coordinate space, which is never rotated. Its bounds, however, will be in landscape
    // because it has a rotation transform applied.
    CGRect bounds = m_pPagingScrollView.bounds;
    CGRect pageFrame = bounds;
    //pageFrame.size.width -= (2 * PADDING);
    //pageFrame.origin.x = (bounds.size.width * index);
    return pageFrame;
}

- (CGSize)contentSizeForPagingScrollView 
{
    // We have to use the paging scroll view's bounds to calculate the contentSize, for the same reason outlined above.
    CGRect bounds = m_pPagingScrollView.bounds;
    return CGSizeMake(bounds.size.width * [self imageCount], bounds.size.height);//因为现在只有两页的设置之后要多
}

#pragma mark -
#pragma mark View loading and unloading
-(void)updatePage
{
	if (m_nCurNewPage != m_nCurPage)
	{
		if(nil != m_pPage)
		{
			if (m_nCurNewPage == m_pPage.nIndex)
			{
				return;
			}
			[m_pPage removeFromSuperview];
			RELEASE_OBJECT(m_pPage);
			
		}
		
		m_nCurPage = m_nCurNewPage;
		[m_pMenuBar UpdatePageNumLabel];
		
		//NSLog(@"frame : %f %f %f %f", m_pPagingScrollView.frame.origin.x, 
//								m_pPagingScrollView.frame.origin.y,
//								m_pPagingScrollView.frame.size.width,
//								m_pPagingScrollView.frame.size.height);
//		
//		NSLog(@"bounds : %f %f %f %f", m_pPagingScrollView.bounds.origin.x, 
//								m_pPagingScrollView.bounds.origin.y,
//								m_pPagingScrollView.bounds.size.width,
//								m_pPagingScrollView.bounds.size.height);
		
		m_pPagingScrollView.bounds = CGRectMake(m_pPagingScrollView.bounds.size.width * m_nCurPage, 0, m_pPagingScrollView.bounds.size.width, m_pPagingScrollView.bounds.size.height);
		
		//NSLog(@"bounds : %f %f %f %f", m_pPagingScrollView.bounds.origin.x, 
//								m_pPagingScrollView.bounds.origin.y,
//								m_pPagingScrollView.bounds.size.width,
//								m_pPagingScrollView.bounds.size.height);
		
		m_pPage = [[ImageScrollView alloc] initWithFrame:[self frameForPageAtIndex:m_nCurNewPage]];
		m_pPage.nIndex = m_nCurNewPage;
		//m_pPage.frame = [self frameForPageAtIndex:m_pPage.nIndex];
		[m_pPage LoadPage:m_pPage.nIndex];
		//[m_pPagingScrollView insertSubview:m_pPage belowSubview:m_pMenuBar];
		[m_pPagingScrollView addSubview:m_pPage];
	}
}

- (void)loadView 
{    
	sharedAudiHistoryViewControllerSingletonManager = self;
	
    // Step 1: make the outer paging scroll view
    CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
    m_pPagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
    m_pPagingScrollView.pagingEnabled = YES;
    m_pPagingScrollView.backgroundColor = [UIColor blackColor];
    m_pPagingScrollView.showsVerticalScrollIndicator = NO;
    m_pPagingScrollView.showsHorizontalScrollIndicator = NO;
    m_pPagingScrollView.contentSize = [self contentSizeForPagingScrollView];
    m_pPagingScrollView.delegate = self;
    //self.view = m_pPagingScrollView;
	self.view = [[UIView alloc] initWithFrame:pagingScrollViewFrame];
    [self.view addSubview:m_pPagingScrollView];
	
	[self initMenuBar];
	[self initIndexBar];
	
	m_nCurPage = -1;
	m_nCurNewPage = -1;
	
	[[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updatePage) userInfo:nil repeats:TRUE] fire];
	
    [self tilePages];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
	RELEASE_OBJECT(m_pPagingScrollView);
	RELEASE_OBJECT(m_pMenuBar);
}

- (void)dealloc
{
	RELEASE_OBJECT(m_pPagingScrollView);
	RELEASE_OBJECT(m_pMenuBar);
    [super dealloc];
}

#pragma mark -
#pragma mark ScrollView delegate methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self tilePages];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}n
*/


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	
	if (UIInterfaceOrientationPortrait == interfaceOrientation)
	{
		return FALSE;
	}
	
	if (UIInterfaceOrientationPortraitUpsideDown == interfaceOrientation)
	{
		return FALSE;
	}
    return TRUE;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}
@end
